package http;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by duan lian on 2018-01-18.
 */
public class HttpConnectionPoolDemo {


	public static void main(String[] args) {
//		HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//		HttpClient httpClient = HttpClients.createMinimal(connectionManager);
//		HttpEntity httpEntity = httpClient.execute(new HttpGet("http://baidu.com")).getEntity();
//		InputStream inputStream = httpEntity
//				.getContent();
//
//		System.out.println(IOUtils.toString(inputStream, Optional.ofNullable(httpEntity.getContentEncoding()).map(poolConnHttpClient -> poolConnHttpClient.getValue()).orElse("utf8")));

		Callable[] fs = {/*() -> newClientPerQuery(),*/ () -> oneSolrClient(), () -> poolConnHttpClient(), () -> basicConnHC()};
		for (Callable func : fs) {
			for (int i = 0; i < 3; i++) {
				try {
					System.out.println(String.format("round: %s, time: %s", i + 1, func.call()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println("----");
		}
	}

	static final SolrQuery solrQuery = new SolrQuery("*:*");
//	static final String solrUrl = "http://localhost:8983/solr/gettingstarted";
	static final String solrUrl = "http://pjhpc:8983/solr/test";
	static final int QUERY_TIMES = 1000000;

	static long newClientPerQuery() throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
		long s = System.currentTimeMillis();
		for (int i = 0; i < QUERY_TIMES; i++) {
			executorService.submit(() -> {
				try {
					new HttpSolrClient.Builder(solrUrl).build().query(solrQuery);
				} catch (SolrServerException | IOException e) {
					e.printStackTrace();
				}
			});
		}

		executorService.shutdown();
		executorService.awaitTermination(120, TimeUnit.SECONDS);

		return System.currentTimeMillis() - s;
	}

	static long oneSolrClient() throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
		HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
		long s = System.currentTimeMillis();
		for (int i = 0; i < QUERY_TIMES; i++) {
			try {
				solrClient.query(solrQuery);
			} catch (SolrServerException | IOException e) {
				e.printStackTrace();
			}
		}
		executorService.shutdown();
		executorService.awaitTermination(120, TimeUnit.SECONDS);
		return System.currentTimeMillis() - s;
	}

	static long poolConnHttpClient() throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
		CloseableHttpClient httpClient = HttpClients.createMinimal(new PoolingHttpClientConnectionManager());
		HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).withHttpClient(httpClient).build();
		long s = System.currentTimeMillis();
		for (int i = 0; i < QUERY_TIMES; i++) {
			try {
				solrClient.query(solrQuery);
			} catch (SolrServerException | IOException e) {
				e.printStackTrace();
			}
		}
		executorService.shutdown();
		executorService.awaitTermination(120, TimeUnit.SECONDS);
		return System.currentTimeMillis() - s;
	}

	static long basicConnHC() throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
		CloseableHttpClient httpClient = HttpClients.createMinimal(new BasicHttpClientConnectionManager());
		HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).withHttpClient(httpClient).build();
		long s = System.currentTimeMillis();
		for (int i = 0; i < QUERY_TIMES; i++) {
			try {
				solrClient.query(solrQuery);
			} catch (SolrServerException | IOException e) {
				e.printStackTrace();
			}
		}
		executorService.shutdown();
		executorService.awaitTermination(120, TimeUnit.SECONDS);
		return System.currentTimeMillis() - s;
	}
}
